//////////////////////////////////////////////////////////////////////////
// FileName:ucSystems
// CreateBy:DragonXiong
// CreateDate:2009-5-4
// Description:
//		
//      
// Modify Log:20091014:Delete the muti language Functions --DragonXiong
//            20100113:Add column IsMenu --DragonXiong
//            20100221:Add  OnReRegisterClick() when LinkButton Click --DragonXiong
//            20100302:Change the data insert sequence --DragonXiong
//	
// Copyright:ASL-MIS
/////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using Model = Auto.Data.Systems.Model;
using BLL = Auto.Data.Systems.BLL;

namespace BaseSystem._System.UserControl
{
    public partial class ucSystems : System.Web.UI.UserControl
    {
        public string SysID
        {
            get {
                   ViewState["SysID"] = txtSysID.Text.Trim();
                   return txtSysID.Text.Trim();
                }
            set { 
                   txtSysID.Text = value; 
                }
        }
        private DataTable Items;

        public delegate void ReRegisterClickEventHandler();
        public event ReRegisterClickEventHandler ReRegisterClick;
        protected void OnReRegisterClick()
        {
            if (ReRegisterClick != null) { ReRegisterClick(); }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            Items = (DataTable)Session["ucSystems.Items"];
            if (!IsPostBack)
            {
                Display();
            }
        }
        private void Display()
        {
            BLL.Systems systems=new BLL.Systems();

            Items = systems.GetSystems(); ;
            BindGridView();
        }

        private void BindGridView()
        {
            GridView1.DataSource = Items;
            GridView1.DataBind();
            Session["ucSystems.Items"] = Items;

            //setGridViewColStyle(GridView1, 0);
        }

        //private void setGridViewColStyle(GridView gridview,int colindex)
        //{
        //    gridview.Columns[colindex].ItemStyle.CssClass = "GridViewColStyle";
        //}

        protected void lkbtnOK_Click(object sender, EventArgs e)
        {
            try
            {
                BLL.Systems bll = new BLL.Systems();

                if (bll.Exists(SysID)) //If the system ID is exists, can't add it again.
                {
                    throw new Exception("The system is already exists,can't add again,please re-input!<br/>If you want to register the system,please find it below,and click register!");
                }
                Model.Systems sysModel = new Model.Systems();
                sysModel.SysID = txtSysID.Text;
                sysModel.SysName = txtSysName.Text;
                sysModel.Note = txtNote.Text;
                bll.Insert(sysModel);
                SetXmlToTable(@"..\" + sysModel.SysID + @"\menu.xml", SysID);
                OnReRegisterClick(); //Add 20100221
            }
            catch (Exception ex)
            {
                lblmsg.Text = ex.Message;
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "ReRegister":
                    int rowIndex = Convert.ToInt32(e.CommandArgument);
                    string sysID = GridView1.Rows[rowIndex].Cells[0].Text;
                    try
                    {
                        SetXmlToTable(@"..\" + sysID + @"\menu.xml",sysID);
                        OnReRegisterClick();
                    }
                    catch (Exception ex)
                    {
                        lblmsg.Text = ex.Message;
                    }
                    break;
                default: break;
            }
        }
        protected void lkbtnNew_Click(object sender, EventArgs e)
        {
            tbNew.Visible = !tbNew.Visible;
        }
        private void SetXmlToTable(string mFilePath, string sysID)
        {
            XmlDocument xmlDoc = new XmlDocument();
            mFilePath = Server.MapPath(mFilePath);
            xmlDoc.Load(mFilePath);
            Model.SysFunctions sysFuncModel = new Model.SysFunctions();
            Convertxml(xmlDoc.DocumentElement,sysID,sysFuncModel);
        }
        private void Convertxml(XmlNode nd, string sysID, Model.SysFunctions sysFucModel)
        {
            LoadRootNode(nd, sysID, sysFucModel);
        }

        private void LoadRootNode(XmlNode xmlNode, string sysID, Model.SysFunctions sysFuncModel)
        {
            BLL.SysFunctions sysFun = new BLL.SysFunctions();
            sysFun.Delete("", sysID, "");//Delete the muti language Functions
            foreach (XmlElement xe in xmlNode.ChildNodes)
            {
                sysFuncModel.CultureInfo = xe.GetAttribute("CultureInfo");
                sysFuncModel.SysID = sysID;
                sysFuncModel.FuncID = sysID + xe.GetAttribute("ID");
                sysFuncModel.FuncName = xe.GetAttribute("Text");
                sysFuncModel.URL= xe.GetAttribute("NavigateUrl");
                sysFuncModel.Target= xe.GetAttribute("Target");
                sysFuncModel.Icon = xe.GetAttribute("ImageUrl");
                //sysFuncModel.IsEnd =xe.GetAttribute("IsEnd")==string.Empty?false:true;

                if (!string.IsNullOrEmpty(xe.GetAttribute("IsEnd")))
                {
                    sysFuncModel.IsEnd = Convert.ToBoolean(Convert.ToInt32(xe.GetAttribute("IsEnd")));
                    
                }
                else
                {
                    sysFuncModel.IsEnd = false;
                }

                //sysFuncModel.IsMenu = xe.GetAttribute("IsMenu") == string.Empty ? true : false;
                if (!string.IsNullOrEmpty(xe.GetAttribute("IsMenu")))
                {
                    sysFuncModel.IsMenu = Convert.ToBoolean(Convert.ToInt32(xe.GetAttribute("IsMenu")));
                }
                else
                {
                    sysFuncModel.IsMenu = true;
                }


                if (xe.ParentNode != null && xe.ParentNode.Attributes.Count > 0) //according the real menu.xml format
                {
                    sysFuncModel.ParentID = xe.ParentNode.Attributes["ID"].Value.ToString() == string.Empty ? null : sysID + xe.ParentNode.Attributes["ID"].Value.ToString();
                }
                else
                {
                    sysFuncModel.ParentID = "";
                }
                
                sysFun.Insert(sysFuncModel);

                if (xe.ChildNodes.Count > 0) //recursion load
                {
                    LoadFunNode(xe, sysID,sysFuncModel);
                }
            }
        }

        private void LoadFunNode(XmlNode xmlNode, string sysID, Model.SysFunctions sysFuncModel)
        {
            foreach (XmlElement xe in xmlNode.ChildNodes)
            {
                sysFuncModel.CultureInfo = xe.GetAttribute("CultureInfo");
                sysFuncModel.SysID = sysID;
                sysFuncModel.FuncID = sysID + xe.GetAttribute("ID");
                sysFuncModel.FuncName = xe.GetAttribute("Text");
                sysFuncModel.URL = xe.GetAttribute("NavigateUrl");
                sysFuncModel.Target = xe.GetAttribute("Target");
                sysFuncModel.Icon = xe.GetAttribute("ImageUrl");
                //sysFuncModel.IsEnd = xe.GetAttribute("IsEnd") == string.Empty ? false : true;
                //sysFuncModel.IsMenu = xe.GetAttribute("IsMenu") == string.Empty ? true : false;
                if (!string.IsNullOrEmpty(xe.GetAttribute("IsEnd")))
                {
                    sysFuncModel.IsEnd = Convert.ToBoolean(Convert.ToInt32(xe.GetAttribute("IsEnd")));
                }
                else
                {
                    sysFuncModel.IsEnd = false;
                }
                if (!string.IsNullOrEmpty(xe.GetAttribute("IsMenu")))
                {
                    sysFuncModel.IsMenu = Convert.ToBoolean(Convert.ToInt32(xe.GetAttribute("IsMenu")));
                }
                else
                {
                    sysFuncModel.IsMenu = true;
                }

                if (xe.ParentNode != null && xe.ParentNode.Attributes.Count>0 )
                {
                    sysFuncModel.ParentID = xe.ParentNode.Attributes["ID"].Value.ToString() == string.Empty ? null : sysID + xe.ParentNode.Attributes["ID"].Value.ToString();
                }
                BLL.SysFunctions sysFun = new BLL.SysFunctions();
                sysFun.Insert(sysFuncModel);
                if (xe.ChildNodes.Count > 0) //recursion load
                {
                    LoadFunNode(xe,sysID,sysFuncModel);
                }

            }
        }
    }
}